# A tibble: 10 × 10
pregnant glucose pressure triceps insulin mass pedigree age diabetes
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <fct>
1 6 148 72 35 0 33.6 0.627 50 pos
2 1 85 66 29 0 26.6 0.351 31 neg
3 8 183 64 0 0 23.3 0.672 32 pos
4 1 89 66 23 94 28.1 0.167 21 neg
5 0 137 40 35 168 43.1 2.29 33 pos
6 5 116 74 0 0 25.6 0.201 30 neg
7 3 78 50 32 88 31 0.248 26 pos
8 10 115 0 0 0 35.3 0.134 29 neg
9 2 197 70 45 543 30.5 0.158 53 pos
10 8 125 96 0 0 0 0.232 54 pos
# ℹ 1 more variable: BMI_class <dbl>
| pregnant | glucose | pressure | triceps | insulin | mass | pedigree | age | diabetes | BMI_class |
|---|---|---|---|---|---|---|---|---|---|
| 6 | 148 | 72 | 35 | 0 | 33.6 | 0.627 | 50 | pos | 3 |
| 1 | 85 | 66 | 29 | 0 | 26.6 | 0.351 | 31 | neg | 2 |
| 8 | 183 | 64 | 0 | 0 | 23.3 | 0.672 | 32 | pos | 2 |
| 1 | 89 | 66 | 23 | 94 | 28.1 | 0.167 | 21 | neg | 2 |
| 0 | 137 | 40 | 35 | 168 | 43.1 | 2.288 | 33 | pos | 4 |
| 5 | 116 | 74 | 0 | 0 | 25.6 | 0.201 | 30 | neg | 2 |
| 3 | 78 | 50 | 32 | 88 | 31.0 | 0.248 | 26 | pos | 3 |
| 10 | 115 | 0 | 0 | 0 | 35.3 | 0.134 | 29 | neg | 3 |
| 2 | 197 | 70 | 45 | 543 | 30.5 | 0.158 | 53 | pos | 3 |
| 8 | 125 | 96 | 0 | 0 | 0.0 | 0.232 | 54 | pos | 1 |
Plotly is an R package for creating interactive web-based graphs via plotly’s JavaScript graphing library, plotly.js.
The plotly R package serializes ggplot2 figures into Plotly’s universal graph JSON. plotly::ggplotly will crawl the ggplot2 figure, extract and translate all of the attributes of the ggplot2 figure into JSON (the colors, the axes, the chart type, etc), and draw the graph with plotly.js. Furthermore, you have the option of manipulating the Plotly object with the style function.
Since the ggplotly() function returns a plotly object, we can manipulate that object in the same way that we would manipulate any other plotly object. A simple and useful application of this is to specify interaction modes, like plotly.js’ layout.dragmode for specifying the mode of click+drag events.
---
title: "Pima Indians Diabetes"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
logo:
source_code: embed
social: menu
---
```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
library(plotly)
library(knitr)
library(DT)
df <- read_csv('https://bryantstats.github.io/math421/data/titanic.csv')
# Create a ggplot object
p <- df %>%
mutate(Survived = factor(Survived)) %>%
ggplot()+
geom_bar(mapping=aes(x=Sex, fill=Survived),
position = 'fill')+
labs(y='Proportion', fill='Survived')
p1 <- df %>%
mutate(Survived = factor(Survived)) %>%
ggplot()+
geom_density(mapping=aes(x=Age, color=Survived))+
facet_wrap(~Pclass)
```
```{r}
library(mlbench)
library(caret)
library(tidyverse)
data(PimaIndiansDiabetes)
df <- tibble(PimaIndiansDiabetes)
# Create a ggplot object
p <- df %>%
mutate(Diabetes = factor(diabetes)) %>%
ggplot()+
geom_bar(mapping=aes(x=age, fill=Diabetes),
position = 'fill')+
labs(y='Proportion', fill='Diabetes')
df$BMI_class <- case_when(df$mass < 18 ~ 1,
((df$mass >= 18) & (df$mass < 30)) ~ 2,
((df$mass >= 30) & (df$mass < 40)) ~ 3,
(df$mass > 40) ~ 4,
TRUE ~ 5)
p1 <- df %>%
mutate(Diabetes = factor(diabetes)) %>%
ggplot()+
geom_density(mapping=aes(x=age, color=Diabetes))+
facet_wrap(~BMI_class)
p2 = df %>%
group_by(BMI_class, diabetes) %>%
count(BMI_class) %>%
ggplot()+
geom_col(mapping=aes(x=factor(BMI_class), y=n, fill = diabetes), position = 'dodge')+
labs(
title = "Diabetes by BMI_class",
x = "BMI Class", # X-axis label
y = "# of Cases", #Y-axis label
)
p3 = df %>%
ggplot()+
geom_point(mapping=aes(x=mass, y=glucose, color = diabetes), position = 'dodge')+
labs(
title = "Mass vs Glucose Levels",
x = "Mass", # X-axis label
y = "Glucose Levels", #Y-axis label
)
```
{.sidebar}
=======================================================================
### 1. Pima Indians Diabetes
Diabetes is something that has been studied by many scientists over the year and has become even more prevalent in America today. This data set contains medical data for female Pima Indians aged 21 and older and can be used to predict whether a person has diabetes based on various features.
### 2. Flexdashboard and Plotly
This interactive uses `flexdashboard` and `plotly` to visualize the data.
Preface Data Set
=======================================================================
Column {data-width=500, .tabset}
-----------------------------------------------------------------------
### Data Preface
```{r}
df1 = head(df, 10)
df1
```
### Data Preface Using Kable
```{r}
kable(df1)
```
### Whole Dataset
```{r}
datatable(df, options = list(
pageLength = 25
))
```
Q2. 4 Main Visuals
=======================================================================
Column {data-width=500, .tabset}
-----------------------------------------------------------------------
#### 4 Visuals
### Visual 1: Proportion of Pos/Neg Diabetes Cases by Age
```{r}
ggplotly(p)
```
### Visual 2: Density of Pos/Neg Cases of Diabetes by Age per BMI Class
```{r}
ggplotly(p1)
```
### Visual 3: Pos/Neg Cases of Diabetes by BMI Class
```{r}
ggplotly(p2)
```
### Visual 4: Mass vs Glucose levels by Pos/Neg Diabetes
```{r}
ggplotly(p3)
```
Plotly Info Tab
=======================================================================
Column {data-width=500}
-----------------------------------------------------------------------
#### 1. Plotly for R
Plotly is an R package for creating interactive web-based graphs via plotly's JavaScript graphing library, plotly.js.
The plotly R package serializes ggplot2 figures into Plotly's universal graph JSON. plotly::ggplotly will crawl the ggplot2 figure, extract and translate all of the attributes of the ggplot2 figure into JSON (the colors, the axes, the chart type, etc), and draw the graph with plotly.js. Furthermore, you have the option of manipulating the Plotly object with the style function.
#### 2. Cutomizing the Layout
Since the ggplotly() function returns a plotly object, we can manipulate that object in the same way that we would manipulate any other plotly object. A simple and useful application of this is to specify interaction modes, like plotly.js' layout.dragmode for specifying the mode of click+drag events.
#### 3. Example
```{r, echo=TRUE, eval=TRUE}
library(plotly)
df <- data.frame(x=c(1, 2, 3, 4), y=c(1, 5, 3, 5), group=c('A', 'A', 'B', 'B'))
p <- ggplot(data=df, aes(x=x, y=y, colour=group)) + geom_point()
ggplotly(p)
```